﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class CMS_Control_RouterBar : System.Web.UI.UserControl
{
    #region 外部属性
    private ShowMode _Type = ShowMode.Type;
    private ShowMode _Border = ShowMode.Border;
    private ShowMode _Article = ShowMode.Article;

    public enum ShowMode : int
    {
        Type = 1,
        Border = 2,
        Article = 3,

    }

    public ShowMode Type
    {
        get
        {
            return _Type;
        }
        set
        {
            _Type = value;
        }
    }


    public ShowMode Border
    {
        get
        {
            return _Border;
        }
        set
        {
            _Border = value;
        }
    }

    public ShowMode Article
    {
        get
        {
            return _Article;
        }
        set
        {
            _Article = value;
        }
    }
    #endregion

    Usas.BLL.Cms.cmsArticle ar = new Usas.BLL.Cms.cmsArticle();
    Usas.BLL.Cms.cmsBorder bo = new Usas.BLL.Cms.cmsBorder();
    Usas.BLL.Cms.cmsType tp = new Usas.BLL.Cms.cmsType();
    Usas.BLL.Cms.cms_purview_setting cps = new Usas.BLL.Cms.cms_purview_setting();
    Usas.Common.Cms.FileControl fc = new Usas.Common.Cms.FileControl();
    Usas.Common.Cms.Role ro = new Usas.Common.Cms.Role();
    Usas.Common.Cms.CurrentInfo cinfo = new Usas.Common.Cms.CurrentInfo();

    Usas.Model.Cms.cmsArticle arm = new Usas.Model.Cms.cmsArticle();
    Usas.Model.Cms.cmsBorder bom = new Usas.Model.Cms.cmsBorder();
    Usas.Model.Cms.cmsType tpm = new Usas.Model.Cms.cmsType();

    string TypeHref = "Default.aspx";
    string BorderHref = "ViewListBorder.aspx";
    string ArticleHref = "ViewListArticle.aspx";
    string Home = "<a href='Default.aspx'>Hyperion</a>";
    private string _BarText;
    private string edit = "";
    private ArrayList ArrEdit = new ArrayList();
    private ArrayList ArrId = new ArrayList();

    /// <summary>
    /// 必须先初始化，才可获得
    /// </summary>
    public string BarText
    {
        get
        {
            return _BarText;
        }
        set
        {
            _BarText = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 导航条初始化
    /// </summary>
    /// <param name="SM">类别</param>
    /// <param name="id">编号</param>
    /// <param name="arr">递归初始化（首次加载New）</param>
    public void IntiBar(ShowMode SM, int id, ArrayList arr)
    {

        DataSet dst = null;
        DataSet ds = null;
        bool edit = false;
        string Bar;
        switch (SM)
        {
            case ShowMode.Article:
                dst = cps.GetList("p_table='a' and p_pid=" + id.ToString());
                ds = ar.GetList("ArtId=" + id);
                if (dst != null)
                {
                    if (dst.Tables[0].Rows.Count > 0)
                    {
                        if (arr.Count <= 0)
                        {
                            string ros = ro.GetSelfSettingRole();
                            ArrEdit.Add(dst.Tables[0].Select(ros + " and p_type='1'", "p_id").Length > 0 ? true : false);
                            //if (ro.GetMd5Str(id + "editTrue").ToString() == Request["check"].ToString())
                            //{
                            //    ArrEdit.Add(true);
                            //}
                            //else
                            //{
                            //    ArrEdit.Add(false);
                            //}
                            ArrId.Add(id);
                            arr.Add(ds.Tables[0].Rows[0]["ArtTopic"].ToString());

                        }
                        else
                        {
                            string ros = ro.GetSelfSettingRole();
                            ArrEdit.Add(dst.Tables[0].Select(ros + " and p_type='1'", "p_id").Length > 0 ? true : false);
                            //if (ro.CheckRole(cinfo.GetEmpRole(), ds.Tables[0].Rows[0]["ArtEditPurview"].ToString(), cinfo.GetEmpId(), false))
                            //{
                            //    ArrEdit.Add(true);
                            //}
                            //else
                            //{
                            //    ArrEdit.Add(false);
                            //}
                            ArrId.Add(ds.Tables[0].Rows[0]["ArtId"].ToString());
                            arr.Add("<a href='" + ArticleHref + "?pid=" + ds.Tables[0].Rows[0]["ArtId"].ToString() + "&checkField'>" + ds.Tables[0].Rows[0]["ArtTopic"].ToString() + "</a>");
                        }
                        if (ds.Tables[0].Rows[0]["ParentArtId"].ToString() == "0")
                        {
                            IntiBar(ShowMode.Border, Convert.ToInt32(ds.Tables[0].Rows[0]["BorderId"]), arr);
                        }
                        else
                        {
                            IntiBar(ShowMode.Article, Convert.ToInt32(ds.Tables[0].Rows[0]["ParentArtId"]), arr);
                        }
                    }
                }
                break;
            case ShowMode.Border:
                dst = cps.GetList("p_table='b' and p_pid=" + id.ToString());
                ds = bo.GetList("BorderId=" + id);
                //ds = bo.GetList("BorderId=" + id);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (arr.Count <= 0)
                        {
                            string ros = ro.GetSelfSettingRole();
                            ArrEdit.Add(dst.Tables[0].Select(ros + " and p_type='1'", "p_id").Length > 0 ? true : false);
                            //if (ro.GetMd5Str(id + "editTrue").ToString() == Request["check"].ToString())
                            //{
                            //    ArrEdit.Add(true);
                            //}
                            //else
                            //{
                            //    ArrEdit.Add(false);
                            //}
                            ArrId.Add(id);
                            arr.Add(ds.Tables[0].Rows[0]["BorderName"].ToString());
                        }
                        else
                        {
                            //if (!edit)
                            //{
                            //    edit = ro.CheckRole(cinfo.GetEmpRole(), ds.Tables[0].Rows[0]["EditPurview"].ToString(), cinfo.GetEmpId(), false);
                            //}

                            //if (ro.CheckRole(cinfo.GetEmpRole(), ds.Tables[0].Rows[0]["EditPurview"].ToString(), cinfo.GetEmpId(), false))
                            //{
                            //    ArrEdit.Add(true);
                            //}
                            //else
                            //{
                            //    ArrEdit.Add(false);
                            //}
                            string ros = ro.GetSelfSettingRole();
                            ArrEdit.Add(dst.Tables[0].Select(ros + " and p_type='1'", "p_id").Length > 0 ? true : false);

                            ArrId.Add(ds.Tables[0].Rows[0]["BorderId"].ToString());
                            arr.Add("<a href='" + ArticleHref + "?bid=" + ds.Tables[0].Rows[0]["BorderId"].ToString() + "&pid=0&checkField'>" + ds.Tables[0].Rows[0]["BorderName"].ToString() + "</a>");
                        }
                        IntiBar(ShowMode.Type, Convert.ToInt32(ds.Tables[0].Rows[0]["TypeId"]), arr);
                    }
                }
                break;
            case ShowMode.Type:
                dst = cps.GetList("p_table='t' and p_pid=" + id.ToString());
                ds = tp.GetList("TypeId=" + id);
                //ds = tp.GetList("TypeId=" + id);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (arr.Count <= 0)
                        {
                            //if (ro.GetMd5Str(id + "editTrue").ToString() == Request["check"].ToString())
                            //{
                            //    ArrEdit.Add(true);
                            //}
                            //else
                            //{
                            //    ArrEdit.Add(false);
                            //}
                            string ros = ro.GetSelfSettingRole();
                            ArrEdit.Add(dst.Tables[0].Select(ros + " and p_type='1'", "p_id").Length > 0 ? true : false);

                            ArrId.Add(id);
                            arr.Add(ds.Tables[0].Rows[0]["TypeName"].ToString());
                        }
                        else
                        {
                            //if (!edit)
                            //{
                            //    edit = ro.CheckRole(cinfo.GetEmpRole(), ds.Tables[0].Rows[0]["EditPurview"].ToString(), cinfo.GetEmpId(), false);
                            //}
                            //if (ro.CheckRole(cinfo.GetEmpRole(), ds.Tables[0].Rows[0]["EditPurview"].ToString(), cinfo.GetEmpId(), false))
                            //{
                            //    ArrEdit.Add(true);
                            //}
                            //else
                            //{
                            //    ArrEdit.Add(false);
                            //}
                            string ros = ro.GetSelfSettingRole();
                            ArrEdit.Add(dst.Tables[0].Select(ros + " and p_type='1'", "p_id").Length > 0 ? true : false);

                            ArrId.Add(ds.Tables[0].Rows[0]["TypeId"].ToString());
                            arr.Add("<a href='" + BorderHref + "?id=" + ds.Tables[0].Rows[0]["TypeId"].ToString() + "&checkField'>" + ds.Tables[0].Rows[0]["TypeName"].ToString() + "</a>");
                        }
                        ShowBar(arr, true);
                    }
                }
                break;
        }


    }

    private void ShowBar(ArrayList arr, bool IsHTML)
    {
        _BarText = "";
        arr.Add(Home);

        litBar.Text = "<div class='bar'>";
        string spilt = "&nbsp;&nbsp;&gt;&nbsp;&nbsp;";
        for (int i = arr.Count - 1; i >= 0; i--)
        {
            if (IsHTML)
            {
                if (i == 0 || i == arr.Count - 1)
                {
                    litBar.Text += arr[i].ToString() + spilt;
                }
                else
                {
                    if (edit == "1")
                    {
                        litBar.Text += arr[i].ToString().Replace("checkField", "check=" + ro.GetMd5Str(ArrId[i].ToString() + "checkTrue")) + spilt;
                    }
                    else
                    {
                        if ((bool)ArrEdit[i])
                        {
                            edit = "1";
                            litBar.Text += arr[i].ToString().Replace("checkField", "check=" + ro.GetMd5Str(ArrId[i].ToString() + "checkTrue")) + spilt;
                        }
                        else
                        {
                            litBar.Text += arr[i].ToString().Replace("checkField", "check=" + ro.GetMd5Str(ArrId[i].ToString() + "checkFalse")) + spilt;
                        }
                    }
                }
            }
            else
            {
                litBar.Text += arr[i].ToString() + spilt;
            }

            _BarText += arr[i].ToString() + spilt;
        }

        litBar.Text = litBar.Text.Substring(0, litBar.Text.Length - spilt.Length);


        _BarText = fc.NoHTML(_BarText.Substring(0, _BarText.Length - spilt.Length));
        litBar.Text += "</div>";

        if (!IsHTML)
        {
            litBar.Text = fc.NoHTML(litBar.Text);
        }
    }


    /// <summary>
    /// 无HTML导航
    /// </summary>
    /// <param name="SM">类别</param>
    /// <param name="id">编号</param>
    /// <param name="arr">首次加载New</param>
    public void IntiBarNoHTML(ShowMode SM, int id, ArrayList arr)
    {
        DataSet ds = null;
        string Bar;
        switch (SM)
        {
            case ShowMode.Article:
                ds = ar.GetList("ArtId=" + id);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (arr.Count <= 0)
                        {

                            ArrEdit.Add(false);

                            ArrId.Add(id);
                            arr.Add(ds.Tables[0].Rows[0]["ArtTopic"].ToString());

                        }
                        else
                        {


                            ArrEdit.Add(false);

                            ArrId.Add(ds.Tables[0].Rows[0]["ArtId"].ToString());
                            arr.Add(ds.Tables[0].Rows[0]["ArtTopic"].ToString());
                        }
                        if (ds.Tables[0].Rows[0]["ParentArtId"].ToString() == "0")
                        {
                            IntiBarNoHTML(ShowMode.Border, Convert.ToInt32(ds.Tables[0].Rows[0]["BorderId"]), arr);
                        }
                        else
                        {
                            IntiBarNoHTML(ShowMode.Article, Convert.ToInt32(ds.Tables[0].Rows[0]["ParentArtId"]), arr);
                        }
                    }
                }
                break;
            case ShowMode.Border:
                ds = bo.GetList("BorderId=" + id);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (arr.Count <= 0)
                        {

                            ArrEdit.Add(false);

                            ArrId.Add(id);
                            arr.Add(ds.Tables[0].Rows[0]["BorderName"].ToString());
                        }
                        else
                        {
                            //if (!edit)
                            //{
                            //    edit = ro.CheckRole(cinfo.GetEmpRole(), ds.Tables[0].Rows[0]["EditPurview"].ToString(), cinfo.GetEmpId(), false);
                            //}


                            ArrEdit.Add(false);


                            ArrId.Add(ds.Tables[0].Rows[0]["BorderId"].ToString());
                            arr.Add(ds.Tables[0].Rows[0]["BorderName"].ToString());
                        }
                        IntiBarNoHTML(ShowMode.Type, Convert.ToInt32(ds.Tables[0].Rows[0]["TypeId"]), arr);
                    }
                }
                break;
            case ShowMode.Type:
                ds = tp.GetList("TypeId=" + id);
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (arr.Count <= 0)
                        {

                            ArrEdit.Add(false);

                            ArrId.Add(id);
                            arr.Add(ds.Tables[0].Rows[0]["TypeName"].ToString());
                        }
                        else
                        {
                            //if (!edit)
                            //{
                            //    edit = ro.CheckRole(cinfo.GetEmpRole(), ds.Tables[0].Rows[0]["EditPurview"].ToString(), cinfo.GetEmpId(), false);
                            //}

                            ArrEdit.Add(false);

                            ArrId.Add(ds.Tables[0].Rows[0]["TypeId"].ToString());
                            arr.Add(ds.Tables[0].Rows[0]["TypeName"].ToString());
                        }
                        ShowBar(arr, false);
                    }
                }
                break;
        }
    }
}
